package video.background.Utils;

//import javafx.scene.input.DataFormat;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @program: background
 * @description:
 * @author: hlt
 * @create: 2023-08-08 16:31
 **/
public class DateTimeUtils {

    static public String dateTimeToString(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = simpleDateFormat.format(date);
//        System.out.println(format);
        return format;
    }

    static public String dateTimeAndSecondToString(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
//        System.out.println(format);
        return format;
    }
    static public String localDateTimeAndSecondToString(){
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return  formatter.format(localDateTime);
    }

    static public LocalDateTime StringToDateTime(String dateString){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 定义日期时间格式
        LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter); // 解析字符串为LocalDateTime对象
        return dateTime;
    }

    // 返回当前时间与目标时间的差距秒数
    static public long getTimeBetween(String targetTime){
        LocalDateTime localDateTime = DateTimeUtils.StringToDateTime(targetTime);
        LocalDateTime now = LocalDateTime.now(); // 获取当前时间
        ZonedDateTime startZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        ZonedDateTime endZonedDateTime = now.atZone(ZoneId.systemDefault());
        Duration between = Duration.between(startZonedDateTime.toInstant(), endZonedDateTime.toInstant());
        return between.getSeconds();
    }
}
